package com.example.parkdemo.parking.add

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.DatePicker
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.base.user.CarColorBean
import com.example.base.user.ParkingResultItem
import com.example.parkdemo.R
import com.example.parkdemo.adapter.CaeImagAdapter
import com.example.parkdemo.adapter.CarColorAdapter
import com.example.parkdemo.databinding.ActivityAddParkingBinding
import com.example.parkdemo.parking.ParkingViewModel
import com.google.gson.Gson
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.text.SimpleDateFormat
import java.util.Calendar

@AndroidEntryPoint
class AddParkingActivity : BaseActivity<ActivityAddParkingBinding, ParkingViewModel>() {
    private lateinit var parkingBeanItem: ParkingResultItem
    lateinit var carColorAdapter: CarColorAdapter
    lateinit var caeImagAdapter: CaeImagAdapter
    override fun initData() {

        vm.getCarColorSuccess.observe(this) {
            val list = Gson().fromJson(it, CarColorBean::class.java)
            carColorAdapter.data.clear()
            carColorAdapter.data.addAll(list)
            carColorAdapter.notifyDataSetChanged()
        }
        vm.AddSuccess.observe(this) {
            Log.i("TagA", "add:$it")
        }
    }

    override fun initView() {

        parkingBeanItem = intent.getSerializableExtra(Const.PARAM_PARKING_BEAN) as ParkingResultItem
        Log.i("TagA", "parkingBean:${parkingBeanItem.parkUser}")

        binding.addParkingHeader.headerTitle.text = "添加车位"
        binding.addParkingHeader.headerBack.setOnClickListener { finish() }

        binding.parkingStartTimeNowTv.text = parkingBeanItem.parkStart
        binding.parkingEndTimeNowTv.text = parkingBeanItem.parkEnd
        binding.parkingTimeNowTv.text = parkingBeanItem.parkTime
        binding.parkingCarNameNowTv.text = parkingBeanItem.parkName

        carColorAdapter = CarColorAdapter()
        binding.parkingColorRv.adapter = carColorAdapter
        binding.parkingColorRv.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        vm.getCarColor()

        carColorAdapter.setOnItemClickListener { adapter, view, position ->
            binding.parkingCarColorSelectIv.setBackgroundColor(Color.parseColor(carColorAdapter.data[position].colorValue))
        }
        binding.parkingStartTimeIv.setOnClickListener {
            showDataDialog(binding.parkingStartTimeNowTv)
        }
        binding.parkingEndTimeIv.setOnClickListener {
            showDataDialog(binding.parkingEndTimeNowTv)
        }
        binding.parkingTimeTv.setOnClickListener {
            showDataDialog(binding.parkingTimeNowTv)
        }
        binding.parkingCarImgRv.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        caeImagAdapter = CaeImagAdapter()
        binding.parkingCarImgRv.adapter = caeImagAdapter


        caeImagAdapter.data.add("+")
        caeImagAdapter.notifyDataSetChanged()

        caeImagAdapter.setOnItemClickListener { adapter, view, position ->
            if (position == caeImagAdapter.data.size - 1) {
                PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            }
        }

        binding.parkingCommentBtn.setOnClickListener {
            if (binding.parkingStartTimeNowTv.text.isNotEmpty() && binding.parkingEndTimeNowTv.text.isNotEmpty() && SPUtils.getInstance()
                    .getString(Const.PARAM_USERNAME) != null && SPUtils.getInstance()
                    .getInt(Const.PARAM_UID) != -1
            ) {
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_PARK_START] = binding.parkingStartTimeNowTv.text.toString()
                map[Const.PARAM_PARK_END] = binding.parkingEndTimeNowTv.text.toString()
                map[Const.PARAM_PARK_NAME] = SPUtils.getInstance().getString(Const.PARAM_USERNAME)
                map[Const.PARAM_PARK_USER] = SPUtils.getInstance().getInt(Const.PARAM_UID)
                map[Const.PARAM_PARK_TIME] = getTime()

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.getAdd(body)

            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bundle = data?.extras
            val bean = bundle?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            val filPath = bean?.path
            if (filPath != null) {
                caeImagAdapter.data.add(0, filPath)
                caeImagAdapter.notifyDataSetChanged()
            }
        }
    }

    private fun showDataDialog(textView: TextView) {
        val calendar = Calendar.getInstance()
        DatePickerDialog(
            this,
            object : OnDateSetListener {
                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                    val currentTimeMillis = System.currentTimeMillis()
                    val format = SimpleDateFormat("HH:mm:ss")
                    val format1 = format.format(currentTimeMillis)
                    textView.text = "$year-$month-$dayOfMonth $format1"
                }
            },
            calendar.get((Calendar.YEAR)),
            calendar.get((Calendar.MONDAY)),
            calendar.get((Calendar.DAY_OF_MONTH))
        ).show()
    }
}